package com.cmnit.tools.utils;

import org.apache.log4j.Logger;

import java.io.InputStream;
import java.util.Properties;

public class ConfigurationManager {
    //创建配置对象
    private static Properties prop = new Properties();
    private static Logger logger = Logger.getLogger(ConfigurationManager.class);

    //通过流读取配置文件
    public static void setResources(String type) {
        InputStream inputStream;
        try {
            if ("product".equals(type)) {
                inputStream = ConfigurationManager.class.getClassLoader().getResourceAsStream("cluster.properties");
                logger.info("加载集群配置文件");
            } else {
                inputStream = ConfigurationManager.class.getClassLoader().getResourceAsStream("local.properties");
                logger.info("加载本地配置文件");
            }
            //加载配置文件
            prop.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取配置项
    public static String getProperty(String key) {
        return prop.getProperty(key);
    }

    //获取配置项
    public static String getProperty(String key, String defaultValue) {
        return prop.getProperty(key, defaultValue);
    }

    //获取布尔类型的配置项
    public static boolean getBoolean(String key) {
        String value = prop.getProperty(key);
        try {
            return Boolean.parseBoolean(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
